#include <curses.h>
/*#define k 3*/
#define DIS_COLS 8
#define COL_MAX 100
#define PRO_NUM 10
#include "f3.h"
/*int MoveBuf(float in[][])
{
	int i,j;
	for(i=0;i<DIS_COLS-1;i++)
	{
		for(j=0;j<3;j++)
		{
			in[i+1][j] = in[i][j];
		}
	}
	return 0;
}*/

int float2graph(float in,int x,int y,int my)
{
	mvaddstr(x,y++,"|");
	float max = COL_MAX;
	int len=(in/max)*(my-y-3),i;

	for(i=y;i<my-1;i++)
		mvaddstr(x,i," ");
	for(i=0;i<len;i++)
	{
		mvprintw(x,y+i,"|");
	}
	mvaddstr(x,y+i," ");
//	attron(A_REVERSE);
	mvprintw(x,y+i+1,"%.1f",in);
//	attroff(A_REVERSE);	
	return 0;	
}
int main()
{
	char cmd[50]="ps aux|sort -nr -k 3|head -";
	char cs[50];
	float cpu[500],max,min,ave,buf[DIS_COLS][3];
	memset(buf,0,DIS_COLS*3);
	int i=1,pid[500],mx,my,x,y;
	initscr();
	noecho();
	getmaxyx(stdscr,mx,my);
//	itoa(my-5,cs,10);
	strcat(cmd,"20");
	x = 2;
	y = 3;
	box(stdscr, ACS_VLINE, ACS_HLINE); /*draw a box*/
	mvaddstr(x,y,"  PID  CPU");
	while(1)
	{

		memset(cpu,-1,500);
		memset(pid,-1,500);
		file2float(cpu,cmd,pid);
		for(i=1;pid[i]!=-1;i++)
		{
			
			mvprintw(x+i,y,"%5d:",pid[i]);
			float2graph(cpu[i],x+i,y+6,my);
//			mvprintw(x+i,y+10,"%.1f",cpu[i]);
		}
		GetMMA(cpu,&max,&min,&ave);
		attron(A_REVERSE);
		mvprintw(x+i,my-30,"Max:%4.1f  Min:%4.1f  Ave:%4.1f",max,min,ave);
		attroff(A_REVERSE);
		refresh();
		sleep(1);
	}
//	getch();
	endwin();
	return 0;
} 
